package intelligentcity.gui;

import intelligentcity.conf.Conf;
import intelligentcity.model.City;
import intelligentcity.model.Point;
import intelligentcity.model.intersections.Intersection;
import intelligentcity.model.streets.OneWayStreet;
import intelligentcity.model.streets.Street;
import intelligentcity.model.turns.Turn;
import intelligentcity.system.LightColor;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

public class CityDrawer {

    public CityDrawer() {

    }

    public static void drawCity(Graphics2D g2d, City city) {
        for (Street street : city.getStreets()) {
            drawStreet(g2d, street);
        }
        for (Intersection intersection : city.getIntersections()) {
            drawIntersection(g2d, intersection);
        }
        for (Turn turn : city.getTurns()) {
            drawTurn(g2d, turn);
        }
        if (Conf.toDrawStartPoints) {
            for (Point startingPoint : city.getStartPoints()) {
                drawStartingPoint(g2d, startingPoint);
            }
        }

    }

    public static void drawOneWayStreet(Graphics2D g2d, OneWayStreet street) {
        g2d.drawLine(street.getP1().getX(), street.getP1().getY(), street.getP2().getX(), street.getP2().getY());
    }

    public static void drawStreet(Graphics2D g2d, Street street) {
        drawOneWayStreet(g2d, street.getFirstStreet());
        drawOneWayStreet(g2d, street.getSecondStreet());
    }

    public static void drawIntersection(Graphics2D g2d, Intersection intersection) {
        drawStreet(g2d, intersection.getVerticalStreet());
        drawStreet(g2d, intersection.getHorizontalStreet());
        if (Conf.toDrawIntersectionRectangle)
            drawIntersectionRectangle(g2d, intersection);
        if (Conf.toDrawLights)
            drawLights(g2d, intersection);
    }

    private static void drawTurn(Graphics2D g2d, Turn turn) {
        drawOneWayStreet(g2d, turn.getLongerHorizontal());
        drawOneWayStreet(g2d, turn.getLongerVertical());
        drawOneWayStreet(g2d, turn.getShorterHorizontal());
        drawOneWayStreet(g2d, turn.getShorterVertical());
    }

    public static void drawPoint(Graphics2D g2d, Point point) {
        g2d.fill(new Ellipse2D.Double(point.getX(), point.getY(), 10, 10));
    }

    public static void drawStartingPoint(Graphics2D g2d, Point point) {
        g2d.setColor(Color.BLUE);
        g2d.fillRect(point.getX() - 5, point.getY() - 5, 10, 10);
        g2d.setColor(Color.BLACK);
    }

    public static void drawIntersectionRectangle(Graphics2D g2d, Intersection intersection) {
        int x = intersection.getHorizontalStreet().getFirstStreet().getP1().getX() + (int) Conf.diameterOfEllipseCar
                / 2;
        int y = intersection.getVerticalStreet().getFirstStreet().getP1().getY() + (int) Conf.diameterOfEllipseCar / 2;
        int width = intersection.getHorizontalStreet().getFirstStreet().getP2().getX() - x
                - (int) Conf.diameterOfEllipseCar / 2;
        int height = intersection.getVerticalStreet().getFirstStreet().getP2().getY() - y
                - (int) Conf.diameterOfEllipseCar / 2;
        g2d.drawRect(x, y, width, height);
    }

    public static void drawLights(Graphics2D g2d, Intersection intersection) {
        int x = intersection.getHorizontalStreet().getFirstStreet().getP1().getX() + (int) Conf.diameterOfEllipseCar
                / 2;
        int y = intersection.getVerticalStreet().getFirstStreet().getP1().getY() + (int) Conf.diameterOfEllipseCar / 2;
        int width = intersection.getHorizontalStreet().getFirstStreet().getP2().getX() - x
                - (int) Conf.diameterOfEllipseCar / 2;
        int height = intersection.getVerticalStreet().getFirstStreet().getP2().getY() - y
                - (int) Conf.diameterOfEllipseCar / 2;

        LightColor color = intersection.getLights().getNorth();
        setColor(g2d, color);
        g2d.fillRect(x + 10, y, width - 20, 10);
        color = intersection.getLights().getSouth();
        setColor(g2d, color);
        g2d.fillRect(x + 10, y + height - 10, width - 20, 10);

        color = intersection.getLights().getEast();
        setColor(g2d, color);
        g2d.fillRect(x, y + 10, 10, height - 20);
        color = intersection.getLights().getWest();
        setColor(g2d, color);
        g2d.fillRect(x + width - 10, y + 10, 10, height - 20);

        g2d.setColor(Color.BLACK);
    }

    private static void setColor(Graphics2D g2d, LightColor color) {
        switch (color) {
            case GREEN:
                g2d.setColor(Color.GREEN);
                break;
            case YELLOW:
                g2d.setColor(Color.YELLOW);
                break;
            case RED:
                g2d.setColor(Color.RED);
                break;
            case RED_YELLOW:
                g2d.setColor(Color.ORANGE);
                break;

        }
    }

}
